using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class ChiNhanhCongTyDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveChiNhanhCongTy(ChiNhanhCongTy chinhanhcongty)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddChiNhanhCongTy", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddChiNhanhCongTy";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, chinhanhcongty.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TenChiNhanh", DbType.String, ParameterDirection.Input, chinhanhcongty.TenChiNhanh);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DiaChiChiNhanh", DbType.String, ParameterDirection.Input, chinhanhcongty.DiaChiChiNhanh);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoDienThoai", DbType.String, ParameterDirection.Input, chinhanhcongty.SoDienThoai);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, chinhanhcongty.IsDelete);
            cmd.Parameters.Add(param);
            param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
            cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			
            result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			return result;
		}
		
		// Get all ChiNhanhCongTy in DB
		public ResultObj GetAllChiNhanhCongTy( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<ChiNhanhCongTy> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllChiNhanhCongTy", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<ChiNhanhCongTy>();
			ChiNhanhCongTy item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new ChiNhanhCongTy();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.TenChiNhanh = Convert.ToString(reader["TenChiNhanh"]);
			item.DiaChiChiNhanh = Convert.ToString(reader["DiaChiChiNhanh"]);
			item.SoDienThoai = Convert.ToString(reader["SoDienThoai"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all ChiNhanhCongTy in DB with condition
		public ResultObj GetAllChiNhanhCongTyByID(ChiNhanhCongTy chinhanhcongty)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<ChiNhanhCongTy> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllChiNhanhCongTyByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, chinhanhcongty.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<ChiNhanhCongTy>();
			ChiNhanhCongTy item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new ChiNhanhCongTy();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.TenChiNhanh = Convert.ToString(reader["TenChiNhanh"]);
			item.DiaChiChiNhanh = Convert.ToString(reader["DiaChiChiNhanh"]);
			item.SoDienThoai = Convert.ToString(reader["SoDienThoai"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a ChiNhanhCongTy out of DB with condition
		public ResultObj DeleteChiNhanhCongTy(ChiNhanhCongTy chinhanhcongty)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteChiNhanhCongTy", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteChiNhanhCongTy";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, chinhanhcongty.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of NhanVien from DB with condition
		public ResultObj GetNhanVien(int IdNhanVien)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<NhanVien> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetNhanVienByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdNhanVien);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<NhanVien>();
			NhanVien item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new NhanVien();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.UserName = Convert.ToString(reader["UserName"]);
			item.Password = Convert.ToString(reader["Password"]);
			item.HoTen = Convert.ToString(reader["HoTen"]);
			item.GioiTinh = Convert.ToInt32(reader["GioiTinh"]);
			item.NgaySinh = Convert.ToDateTime(reader["NgaySinh"]);
			item.Email = Convert.ToString(reader["Email"]);
			item.DienThoaiDiDong = Convert.ToString(reader["DienThoaiDiDong"]);
			item.DienThoaiNha = Convert.ToString(reader["DienThoaiNha"]);
			item.CMND = Convert.ToString(reader["CMND"]);
			item.NgayCapCMND = Convert.ToDateTime(reader["NgayCapCMND"]);
			item.NoiCapCMND = Convert.ToString(reader["NoiCapCMND"]);
			item.DiaChiThuongTru = Convert.ToString(reader["DiaChiThuongTru"]);
			item.DiaChiTamTru = Convert.ToString(reader["DiaChiTamTru"]);
			item.DanToc = Convert.ToString(reader["DanToc"]);
			item.TonGiao = Convert.ToString(reader["TonGiao"]);
			item.HocVan = Convert.ToString(reader["HocVan"]);
			item.NgayVaoLam = Convert.ToDateTime(reader["NgayVaoLam"]);
			item.IdChucVu = Convert.ToInt32(reader["IdChucVu"]);
			item.IdChiNhanhCTy = Convert.ToInt32(reader["IdChiNhanhCTy"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get info of PhieuNhapKho from DB with condition
		public ResultObj GetPhieuNhapKho(int IdPhieuNhapKho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuNhapKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetPhieuNhapKhoByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdPhieuNhapKho);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuNhapKho>();
			PhieuNhapKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuNhapKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdBangKe = Convert.ToInt32(reader["IdBangKe"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.NguoiLap = Convert.ToString(reader["NguoiLap"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.KeToanTruong = Convert.ToString(reader["KeToanTruong"]);
			item.NguoiGiaoHang = Convert.ToString(reader["NguoiGiaoHang"]);
			item.NoiDung = Convert.ToString(reader["NoiDung"]);
			item.SoHoaDonDo = Convert.ToString(reader["SoHoaDonDo"]);
			item.Thue = Convert.ToInt64(reader["Thue"]);
			item.IdChiNhanhCTy = Convert.ToInt32(reader["IdChiNhanhCTy"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
					
	}
}
